package binarysearch

//二分查找
func search(nums []int, target int) int {
	if len(nums) == 0 {
		return -1   //无
	}
	//区间定义为【left,right】 ,则len(nums)==1是有意义的
	left, right :=0, len(nums)-1
	for left <= right {
		mid := (left + right)/2
		if nums[mid] > target {
			//则在左边查找
			right = mid - 1
		}else if nums[mid] < target {
			left = mid + 1
		}else {
			return mid
		}
	}
	return -1
}

